//
//  NewShopCartBottomView.m
//  FD
//
//  Created by xiaofang wang on 2018/11/19.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  欧元 €


#import "NewShopCartBottomView.h"
@interface NewShopCartBottomView()
/**1.购物车按钮btn*/
@property (nonatomic, strong) UIButton *cartBtn;
/**2019-2-26添加一张:大透明按钮*/
@property (nonatomic, strong) UIButton *bigLucencyBtn;

/**3.价格lb 使用富文本*/
@property (nonatomic, strong) UILabel *priceLb;
/**配送时间view*/
@property (nonatomic,  strong) UILabel *timeLb;
/**6.去结算 按钮*/
@property (nonatomic, strong) UIButton *jieSuanBtn;
/**跟android一样*/
@property (nonatomic, strong) UILabel *VIEWBASKETLB;

@end

@implementation NewShopCartBottomView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    [self addSubview:self.cartBtn]; //0.购物图标
    
    [self addSubview:self.priceLb]; //1.价格
    [self addSubview:self.timeLb]; //2.时间
    [self addSubview:self.VIEWBASKETLB]; //3.很android保持一致
    [self addSubview:self.jieSuanBtn]; //4.结算按钮
    
    //2019-2-25 大透明按钮
    [self addSubview:self.bigLucencyBtn];
}


#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    //0.购物车按钮
    [self.cartBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(50, 50));
        make.left.equalTo(self.mas_left);//.offset(10);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    
    //2019-2-25 大透明按钮
    [self.bigLucencyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self);
        make.right.equalTo(self.jieSuanBtn.mas_left);
    }];
    
    
    //1.价格
    [self.priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cartBtn.mas_right);
        make.top.equalTo(self.mas_top).offset(5);
        make.height.mas_equalTo(22);
        make.width.greaterThanOrEqualTo(@40); //1-2自适应文字宽度
    }];
    
    //2.时间
    [self.timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.priceLb.mas_right).offset(5);
        make.top.equalTo(self.mas_top).offset(5);
        make.height.mas_equalTo(22);
        //5s长度过长，如果是5、5s就固定宽度
        if (SCREEN_WIDTH <= 320.0) {
            make.width.mas_equalTo(130);
        }else if (SCREEN_WIDTH >320.0 && SCREEN_WIDTH <= 375.0) { //6、7、8s 、x
             make.width.mas_equalTo(160);
        }
    }];
    
    //3.VIEW BASKET
    [self.VIEWBASKETLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.priceLb.mas_left);
        make.right.equalTo(self.timeLb.mas_right);
        make.top.equalTo(self.timeLb.mas_bottom);
        make.height.mas_equalTo(22);
    }];

    [self.jieSuanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(90);
        make.top.bottom.right.equalTo(self);
    }];
    [self.jieSuanBtn leftTextRightImageWithMargin:5]; //12-31左文字右图片
}

#pragma mark - getter --- ↓↓↓↓↓↓↓↓↓
- (UIButton *)cartBtn {
    if (!_cartBtn) {
        _cartBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_cartBtn setImage:[UIImage imageNamed:@"gwc"] forState:(UIControlStateNormal)];
        //2019-2-25 不要了点击范围太小了 换成下面的那个
//        [_cartBtn addTarget:self action:@selector(clickCartBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _cartBtn;
}

/**2019-2-*25大透明按钮上的小按钮不要了*/
- (UIButton *)bigLucencyBtn {
    if (!_bigLucencyBtn) {
        _bigLucencyBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_bigLucencyBtn addTarget:self action:@selector(clickCartBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
//        _bigLucencyBtn.backgroundColor = [UIColor yellowColor];
    }
    return _bigLucencyBtn;
}



- (UILabel *)priceLb {
    if (!_priceLb) {
        _priceLb = [UILabel new];
        _priceLb.font = [UIFont systemFontOfSize:16.0];
        _priceLb.textColor = kMainRedColor;
        _priceLb.text = @"€0";
    }
    return _priceLb;
}

- (UIButton *)jieSuanBtn {
    if (!_jieSuanBtn) {
        _jieSuanBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_jieSuanBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        NSString *qjsStr = NSLocalizedString(Order_Settlement, @"去结算");
        
        [_jieSuanBtn setTitle:[NSString stringWithFormat:@"%@",qjsStr] forState:(UIControlStateNormal)];
        _jieSuanBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [_jieSuanBtn setImage:[UIImage imageNamed:@"leftArrow"] forState:(UIControlStateNormal)];//右箭头
        _jieSuanBtn.backgroundColor = kMainRedColor;
        [_jieSuanBtn addTarget:self action:@selector(clickJieSuanBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _jieSuanBtn;
}

- (UILabel *)timeLb {
    if (!_timeLb) {
        _timeLb = [UILabel new];
        _timeLb.backgroundColor = rgba(170, 170, 170, 1.0);
        _timeLb.textColor = [UIColor whiteColor];
        _timeLb.font = [UIFont systemFontOfSize:10.0*kScaleFit];
        _timeLb.layer.cornerRadius = 5;
        _timeLb.layer.masksToBounds = YES;
    }
    return _timeLb;
}
//跟android一样
- (UILabel *)VIEWBASKETLB {
    if (!_VIEWBASKETLB) {
        _VIEWBASKETLB = [[UILabel alloc] init];
        _VIEWBASKETLB.text = @"VIEW BASEKET";
        _VIEWBASKETLB.font = [UIFont systemFontOfSize:15.0];
    }
    return _VIEWBASKETLB;
}

#pragma mark - 事件监听
/**点击购物车按钮*/
- (void)clickCartBtnAction:(UIButton *)btn {
    FDLog(@"点击购物车按钮 %s--%d",__FUNCTION__,__LINE__);
    if ([self.delegate respondsToSelector:@selector(shopcartViewDidClickCartBtn:)]) {
        [self.delegate shopcartViewDidClickCartBtn:btn];
    }
}

/**去结算*/
- (void)clickJieSuanBtnAction:(UIButton *)btn {
    //直接跳转页面的无需考虑重复点击问题
    if ([self.delegate respondsToSelector:@selector(shopcartViewDidClickJieSuanBtnAction:)]) {
        [self.delegate shopcartViewDidClickJieSuanBtnAction:btn];
    }
}


/**3.更新总价*/
- (void)updateshopcartBottomViewTotalPrice:(CGFloat)totalPrice {
    //2019-2-16更android保持一致
    NSString *priceStr = @"€0";
    if (totalPrice <=0.1) {
        priceStr = @"€0";
    }else {
        priceStr = [NSString stringWithFormat:@"€%.1f",totalPrice];
    }
    self.priceLb.text = priceStr;
    
    //计算文字宽度
//    CGSize size = [priceStr sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0]}];
//    //更新时间按钮约束
//    [self.priceLb mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.cartBtn.mas_right);
//        make.top.equalTo(self.mas_top).offset(5);
//        make.size.mas_equalTo(CGSizeMake(size.width, 22));
//    }];
}

/**4.更新营业时间*/
- (void)updateYingYeShiJianBegin:(NSString *)begin end:(NSString *)end{
    
    //背景颜色红，文字颜色白
    self.timeLb.textColor = [UIColor whiteColor];
    self.timeLb.backgroundColor = kMainRedColor;
    
    self.timeLb.text = [NSString stringWithFormat:@"Open Hour%@am~%@pm",begin,end];
    
//    opening time
}
@end

